package com.kaiyiweitong.tongxing.activity.classify;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kaiyiweitong.tongxing.R;
import com.kaiyiweitong.tongxing.base.BaseActivity;
import com.kaiyiweitong.tongxing.fragment.service_info.ServiceDetailFragment;
import com.kaiyiweitong.tongxing.fragment.service_info.ServiceEvaluateFragment;
import com.kaiyiweitong.tongxing.fragment.service_info.SpecificationFragment;
import com.kaiyiweitong.tongxing.other.FragmentFactory;
import com.kaiyiweitong.tongxing.util.CommonUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/12/19 0019.
 */

public class ServiceInfoActivity extends BaseActivity implements ViewPager.OnPageChangeListener {
    @Bind(R.id.title)
    TextView mTitle;
    @Bind(R.id.pic)
    ImageView mPic;
    @Bind(R.id.service_name)
    TextView mServiceName;
    @Bind(R.id.price)
    TextView mPrice;
    @Bind(R.id.like)
    ImageView mLike;
    @Bind(R.id.introduce)
    TextView mIntroduce;
    @Bind(R.id.specification)
    TextView mSpecification;
    @Bind(R.id.service_detail)
    TextView mServiceDetail;
    @Bind(R.id.service_evaluate)
    TextView mServiceEvaluate;
    @Bind(R.id.buy_now)
    TextView mBuyNow;
    @Bind(R.id.vp_service_info)
    ViewPager mVpServiceInfo;

    private List<Fragment> mFragments = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_service_info);
        ButterKnife.bind(this);
        init();
    }

    private void init() {
        LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) mVpServiceInfo.getLayoutParams();
        layoutParams.height = CommonUtil.getScreenWidth(this);
        mVpServiceInfo.setLayoutParams(layoutParams);

        mFragments.add(FragmentFactory.getInstance(SpecificationFragment.class));
        mFragments.add(FragmentFactory.getInstance(ServiceDetailFragment.class));
        mFragments.add(FragmentFactory.getInstance(ServiceEvaluateFragment.class));
        FragmentPagerAdapter adapter = new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }
        };
        mVpServiceInfo.setAdapter(adapter);
        mVpServiceInfo.setOffscreenPageLimit(2);
        mVpServiceInfo.addOnPageChangeListener(this);
        indicator(0);
    }

    @OnClick({R.id.like, R.id.specification, R.id.service_detail, R.id.service_evaluate, R.id.pay_money})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.like:
                CommonUtil.toast(this, "收藏成功!");
                break;
            case R.id.specification:
                indicator(0);
                break;
            case R.id.service_detail:
                indicator(1);
                break;
            case R.id.service_evaluate:
                indicator(2);
                break;
            case R.id.pay_money:

                break;
        }
    }

    private int currentIndex = -1;

    private synchronized void indicator(int position) {
        if (currentIndex != position) {
            mVpServiceInfo.setCurrentItem(position);
            currentIndex = position;
            if (position == 0) {
                mSpecification.setSelected(true);
                mServiceDetail.setSelected(false);
                mServiceEvaluate.setSelected(false);
            } else if (position == 1) {
                mSpecification.setSelected(false);
                mServiceDetail.setSelected(true);
                mServiceEvaluate.setSelected(false);
            } else {
                mSpecification.setSelected(false);
                mServiceDetail.setSelected(false);
                mServiceEvaluate.setSelected(true);
            }
        }
    }

    @Override
    public void onPageSelected(int position) {
        indicator(position);
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }
}
